package com.surfilter.massdata.es.core.client.pool;

import org.apache.commons.pool.BasePoolableObjectFactory;
import org.apache.log4j.Logger;

import com.surfilter.massdata.es.core.client.EsClient;
import com.surfilter.massdata.es.core.client.EsEnv;

public class PoolableUserFactory extends BasePoolableObjectFactory<EsClient>{

	private EsEnv esEnv = null;
	
	protected final Logger logger = Logger.getLogger(getClass());
	
	public PoolableUserFactory(EsEnv esEnv) {
		super();
		this.esEnv = esEnv;
	}
	
	@Override
	public EsClient makeObject() throws Exception {
		EsClient esClient = new EsClient(esEnv);
		logger.info("create EsClient! " + esClient);
		return esClient;
	}

	@Override
	public void destroyObject(EsClient esClient) throws Exception {
		esClient.close();
		logger.info("destroyObject EsClient! " + esClient);
		super.destroyObject(esClient);
	}

	@Override
	public void passivateObject(EsClient obj) throws Exception {
		logger.info("return to pool");
		super.passivateObject(obj);
	}

}
